/*
 * Created on Feb 17, 2005
 */
package com.scalar.chiptrack.finance.openpo.forms;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.commons.Address;
import com.scalar.chiptrack.commons.ClientInfo;
import com.scalar.chiptrack.sales.SalesOrder;
import com.scalar.chiptrack.utils.StringUtils;


/**
 * @author Ravikanth
**/
public class OpenPurchaseOrderForm extends ActionForm
{
	private int vendor_id			= -1;
	private int purchaseOrder_id	= -1;
	private int	poType 				= -1;
	private int poRev 				= -1;
	private int financeHold 		= -1;
	private int operationsHold 		= -1;
	private String vendorName 		= null;
	private String poNumber 		= null;
	private String poDate 			= null;
	private String poStatus			= null;
	private String poMaxAmt 		= null;
	private String vendorQuoteNumber= null;
	private String validFromDate 	= null;
	private String validToDate 		= null;
	private String poLastModifiedDate 	= null;
	private String poIssuedAmt 		= null;
	private String poRemainingAmt 	= null;
	private int billingAddress_id 	= -1;
	private int shippingAddress_id  = -1;
	private Address billingAddress 	= null;
	private Address shippingAddress = null;
	private String comments 		= null;
	private String contactPerson	= null;
    private String vendorAddressId  = null;

    private ArrayList shippingContactPersonList = null;
    private ArrayList shippingVendorsList = null;
    private ArrayList shippingVendorAddressList = null;
    private ArrayList shippingAddressList = null;
    private String clientAddressAsStringForHTML = null;
    private ClientInfo clientInfo = null;

    private boolean cancel;

    //added by Murali Krishna on 24/3/05 prevCancel is loaded with the same value as cancel while loading open po from database
    private boolean prevCancel;
    private String path = null;
    private int shippingMethodId;
    private String payTerms;
    private ArrayList shippingMethodsList;
    private ArrayList payTermsList;
    private String newShippingMethod;
    private String newShippingAccount;
    private String newPayTerms;
    private String shipMethod;
    /*
        used to display the client address in HTML printable formats
        CAUTION : THE STRING RETURNED BY THIS METHOD WILL NOT HAVE EMAIL
    */
    public String getClientAddressAsStringForHTML() {
        return clientAddressAsStringForHTML;
    }

    public void setClientAddressAsStringForHTML(String clientAddressAsStringForHTML) {
        this.clientAddressAsStringForHTML = clientAddressAsStringForHTML;
    }

    public ClientInfo getClientInfo() {
        return clientInfo;
    }

    public void setClientInfo(ClientInfo clientInfo) {
        this.clientInfo = clientInfo;
        if( clientInfo != null)
        {
            Address clientAddress = clientInfo.getAddress();
            String tmpString = SalesOrder.getClientAddressAsString( clientAddress );
            this.setClientAddressAsStringForHTML( tmpString.trim().replaceAll("\n","<BR>"));
        }
    }

	public OpenPurchaseOrderForm()
	{

	}

	/**
	 * @return
	 */
	public Address getBillingAddress()
	{
		return billingAddress;
	}

	/**
	 * @return
	 */
	public String getComments()
	{
		return comments;
	}

    public String getCommentsForHTML()
    {
        String returnStr = "";
        if( this.getComments() != null)
        {
            returnStr = this.getComments().trim().replaceAll("\n","<BR>");
        }
        return returnStr;

    }

	/**
	 * @return
	 */
	public int getFinanceHold()
	{
		return financeHold;
	}

	/**
	 * @return
	 */
	public int getOperationsHold()
	{
		return operationsHold;
	}

	/**
	 * @return
	 */
	public String getPoDate()
	{
		return poDate;
	}

	/**
	 * @return
	 */
	public String getPoIssuedAmt()
	{
		return poIssuedAmt;
	}

	/**
	 * @return
	 */
	public String getPoLastModifiedDate()
	{
		return poLastModifiedDate;
	}

	/**
	 * @return
	 */
	public String getPoMaxAmt()
	{
		return poMaxAmt;
	}

	/**
	 * @return
	 */
	public String getPoNumber()
	{
		return poNumber;
	}

	/**
	 * @return
	 */
	public String getPoRemainingAmt()
	{
		if( poMaxAmt == null )
		{
			return "0";
		}
		else if( poIssuedAmt == null )
		{
			return poMaxAmt;
		}
		else
		{
			return StringUtils.formatAmount( (Double.parseDouble(poMaxAmt) - Double.parseDouble(poIssuedAmt)),"#######.##");
		}
	}

	/**
	 * @return
	 */
	public int getPoRev()
	{
		return poRev;
	}

	/**
	 * @return
	 */
	public int getPoType()
	{
		return poType;
	}

	/**
	 * @return
	 */
	public int getPurchaseOrder_id()
	{
		return purchaseOrder_id;
	}

	/**
	 * @return
	 */
	public String getValidFromDate()
	{
		return validFromDate;
	}

	/**
	 * @return
	 */
	public String getValidToDate()
	{
		return validToDate;
	}

	/**
	 * @return
	 */
	public int getVendor_id()
	{
		return vendor_id;
	}

	/**
	 * @return
	 */
	public String getVendorName()
	{
		return vendorName;
	}

	/**
	 * @return
	 */
	public String getVendorQuoteNumber()
	{
		return vendorQuoteNumber;
	}

	/**
	 * @param address
	 */
	public void setBillingAddress(Address address)
	{
		billingAddress = address;
	}

	/**
	 * @param string
	 */
	public void setComments(String string)
	{
		comments = string;
	}

	/**
	 * @param i
	 */
	public void setFinanceHold(int i)
	{
		financeHold = i;
	}

	/**
	 * @param i
	 */
	public void setOperationsHold(int i)
	{
		operationsHold = i;
	}

	/**
	 * @param string
	 */
	public void setPoDate(String string)
	{
		poDate = string;
	}

	/**
	 * @param string
	 */
	public void setPoIssuedAmt(String string)
	{
		poIssuedAmt = string;
	}

	/**
	 * @param string
	 */
	public void setPoLastModifiedDate(String string)
	{
		poLastModifiedDate = string;
	}

	/**
	 * @param string
	 */
	public void setPoMaxAmt(String string)
	{
		poMaxAmt = string;
	}

	/**
	 * @param string
	 */
	public void setPoNumber(String string)
	{
		poNumber = string;
	}

	/**
	 * @param string
	 */
	public void setPoRemainingAmt(String string)
	{
		poRemainingAmt = string;
	}

	/**
	 * @param i
	 */
	public void setPoRev(int i)
	{
		poRev = i;
	}

	/**
	 * @param i
	 */
	public void setPoType(int i)
	{
		poType = i;
	}

	/**
	 * @param i
	 */
	public void setPurchaseOrder_id(int i)
	{
		purchaseOrder_id = i;
	}

	/**
	 * @param string
	 */
	public void setValidFromDate(String string)
	{
		validFromDate = string;
	}

	/**
	 * @param string
	 */
	public void setValidToDate(String string)
	{
		validToDate = string;
	}

	/**
	 * @param i
	 */
	public void setVendor_id(int i)
	{
		vendor_id = i;
	}

	/**
	 * @param string
	 */
	public void setVendorName(String string)
	{
		vendorName = string;
	}

	/**
	 * @param string
	 */
	public void setVendorQuoteNumber(String string)
	{
		vendorQuoteNumber = string;
	}

    public boolean isCancel() {
        return cancel;
    }

    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }

    public boolean isPrevCancel() {
        return prevCancel;
    }

    public void setPrevCancel(boolean prevCancel) {
        this.prevCancel = prevCancel;
    }

	public String getPOTypeString()
	{
		switch ( poType )
		{
			case 1:
				return "Open";
			case 2:
				return "Standard PO";
			case 3:
				return "Drop Ship PO";
		}
		return null;
	}

	/**
	 * For resetting the PurchaseOrderSearchForm Values
	 * @param mapping
	 * @param request
	 */
	public void reset( ActionMapping mapping, HttpServletRequest request )
	{
		this.poType = 1;
		this.financeHold = -1;
		this.operationsHold = -1;
        this.cancel = false;
	}
	/**
	 * @return
	 */
	public Address getShippingAddress()
	{
		return shippingAddress;
	}

	/**
	 * @param address
	 */
	public void setShippingAddress(Address address)
	{
		shippingAddress = address;
	}

	/**
	 * @return
	 */
	public int getBillingAddress_id()
	{
		return billingAddress_id;
	}

	/**
	 * @return
	 */
	public int getShippingAddress_id()
	{
		return shippingAddress_id;
	}

	/**
	 * @param i
	 */
	public void setBillingAddress_id(int i)
	{
		billingAddress_id = i;
	}

	/**
	 * @param i
	 */
	public void setShippingAddress_id(int i)
	{
		shippingAddress_id = i;
	}

	public String getShippingAddressAsString()
	{
		return formateAddress( shippingAddress );
	}

	public String getBillingAddressAsString()
	{
		return formateAddress( billingAddress );
	}

	public String formateAddress( Address address )
	{
		StringBuffer formatedAddress = new StringBuffer();
		formatedAddress.append( formatAddressAsStringForHTML( address ) );
		return formatedAddress.toString();
	}

	/**
	 *
	 * @param address Address object to format
	 * @return formatted address as String or null
	 */
	public String formatAddressAsStringForHTML( Address address )
	{
		if ( address != null )
		{
			StringBuffer tempAddress = new StringBuffer( "" );
			String contactPerson = address.getContactPerson();
			String address1 = address.getAddress1();
			String address2 = address.getAddress2();
			String address3 = address.getAddress3();
			String address4 = address.getAddress4();
			String city = address.getCity();
			String zipCode = address.getZipCode();
			String state = address.getState();
			String country = address.getCountry();
			String phoneNumber = address.getPhoneNumber();
			String cellNumber = address.getCellNumber();
			String faxNumber = address.getFaxNumber();
			String email = address.getEmail();

			if( contactPerson != null && !( contactPerson.trim().equals( "" ) ) )
			{
				tempAddress.append( contactPerson + "\n" );
			}
			if( address1 != null && !( address1.trim().equals( "" ) ) )
			{
				tempAddress.append( address1 + "\n" );
			}
			if( address2 != null && !( address2.equals( "" ) ) )
			{
				tempAddress.append( address2 + "\n" );
			}
			if( address3 != null && !( address3.equals( "" ) ) )
			{
				tempAddress.append( address3 + "\n" );
			}
			if( address4 != null && !( address4.equals( "" ) ) )
			{
				tempAddress.append( address4 + "\n" );
			}
			if( city != null && !( city.equals( "" ) ) )
			{
				tempAddress.append( city + "\n" );
			}
			if( state != null && !( state.equals( "" ) ) )
			{
				tempAddress.append( state );
			}


			if( zipCode != null && !( zipCode.equals( "" ) ) )
			{
				if( state != null && !( state.equals( "" ) ) )
				{
					tempAddress.append( " - " );
				}
				tempAddress.append( zipCode );
			}

			if( country != null && !( country.equals( "" ) ) )
			{
				if( ( zipCode != null && !( zipCode.equals( "" ) ) ) || ( state != null && !( state.equals( "" ) ) ) )
				{
					tempAddress.append( ", " );
				}
				tempAddress.append( country );
			}
            try
            {
                String tmpStr = tempAddress.substring( tempAddress.length()-1);
                if ( !tmpStr.equals("\n"))
                {
                    tempAddress.append( "\n");
                }
            }
            catch( Exception e)
            {
                tempAddress = new StringBuffer("");
            }

			tempAddress.append( ( phoneNumber != null && !( phoneNumber.equals( "" ) ) ) ? "\nTel:" + phoneNumber : "" );
			tempAddress.append( ( cellNumber != null && !( cellNumber.equals( "" ) ) ) ? "\nCell:" + cellNumber : "" );
			tempAddress.append( ( faxNumber != null && !( faxNumber.equals( "" ) ) ) ? "\nFax:" + faxNumber : "" );
			tempAddress.append( ( email != null && !( email.equals( "" ) ) ) ? "\nEmail:" + email : "" );

			return tempAddress.toString();
		}
		return "";
	}

	/**
	 *
	 * @param address Address object to format
	 * @param addressType This parameter represents the node name in the XML file, in which the actual address is encapsulated
	 * @return formatted address as String or null
	 */
	public String formatAddressAsStringForXML( Address address, String addressType )
	{
		if( address != null )
		{
			StringBuffer buf = new StringBuffer();
			String address1 = "";
			String address2 = "";
			String address3 = "";
			String address4 = "";

			if ( !(address.getAddress1()==null || address.getAddress1().trim().length() == 0) )
			{
				address1 =  address.getAddress1();
			}
			if ( !(address.getAddress2()==null || address.getAddress2().trim().length() == 0) )
			{
				address2 =  address.getAddress2();
			}
			if ( !(address.getAddress3()==null || address.getAddress3().trim().length() == 0) )
			{
				address3 =  address.getAddress3();
			}
			if ( !(address.getAddress4()==null || address.getAddress4().trim().length() == 0) )
			{
				address4 =  address.getAddress4();
			}
			buf.append ( "<"+addressType+">" + "\n" );
			if( address.getContactPerson() != null && address.getContactPerson().trim().length() !=0 )
			{
			buf.append ( "<contactPerson><![CDATA[" + address.getContactPerson () + "]]></contactPerson>" + "\n" );
			}
			buf.append ( "<address1><![CDATA[" + address1 + "]]></address1>" + "\n" );
			buf.append ( "<address2><![CDATA[" + address2 + "]]></address2>" + "\n" );
			buf.append ( "<address3><![CDATA[" + address3 + "]]></address3>" + "\n" );
			buf.append ( "<address4><![CDATA[" + address4 + "]]></address4>" + "\n" );
			buf.append ( "<city><![CDATA["+ address.getCity () + "]]></city>" + "\n" );
			buf.append ( "<stateZipCountry><![CDATA[" );
			if(address.getState ()!=null && !(address.getState ().equals("")))
			{
				buf.append(address.getState ());
			}
			if(address.getZipCode()!=null && !(address.getZipCode().equals("")))
			{
				if(address.getState ()!=null && !(address.getState ().equals("")))
				{
					buf.append(" - ");
				}
				buf.append(address.getZipCode());
			}
			if(address.getCountry ()!=null && !(address.getCountry ().equals("")))
			{
				if((address.getZipCode()!=null && !(address.getZipCode().equals("")))||(address.getState ()!=null && !(address.getState ().equals(""))))
				{
					buf.append(", ");
				}
				buf.append(address.getCountry ());
			}
			buf.append ( "]]></stateZipCountry>" + "\n" );
			buf.append ( "<phone><![CDATA[" + ( address.getPhoneNumber () != null && !address.getPhoneNumber().equals("") ?  "Tel:"+ address.getPhoneNumber () : "" ) + "]]></phone>" + "\n" );
			buf.append ( "<cell><![CDATA[" + ( address.getCellNumber () != null && !address.getCellNumber().equals("") ?  "Cell:"+address.getCellNumber () : "" ) + "]]></cell>" + "\n" );
			buf.append ( "<fax><![CDATA[" + ( address.getFaxNumber () != null && !address.getFaxNumber().equals("") ?  "Fax:" + address.getFaxNumber () : "" ) + "]]></fax>" + "\n" );
			buf.append ( "<email><![CDATA[" + ( address.getEmail () != null && !address.getEmail().equals("") ?  "EMail:" + address.getEmail () : "" ) + "]]></email>" + "\n" );
			buf.append ( "</"+addressType+">" + "\n" );
			return buf.toString() ;
		}
		return null;
	}


	/**
	 * @return
	 */
	public String getPoStatus()
	{
		return poStatus;
	}

	/**
	 * @param string
	 */
	public void setPoStatus(String string)
	{
		poStatus = string;
	}

	/**
	 * @return
	 */
	public String getContactPerson()
	{
		return contactPerson;
	}

	/**
	 * @param string
	 */
	public void setContactPerson(String string)
	{
		contactPerson = string;
	}

	/**
	 * @return
	 */
	public ArrayList getShippingAddressList()
	{
		if( shippingAddressList != null )
			return shippingAddressList;
		else
			return new ArrayList();
	}

	/**
	 * @return
	 */
	public ArrayList getShippingContactPersonList()
	{
		if( shippingAddressList != null )
			return shippingContactPersonList;
		else
			return new ArrayList();
	}

	/**
	 * @param list
	 */
	public void setShippingAddressList(ArrayList list)
	{
		shippingAddressList = list;
	}

	/**
	 * @param list
	 */
	public void setShippingContactPersonList(ArrayList list)
	{
		shippingContactPersonList = list;
	}

	public ArrayList getFormattedShippingAddressList()
	{
		ArrayList formattedArrayList = new ArrayList();

		if( shippingAddressList != null && shippingAddressList.isEmpty() == false )
		{
			for( int i = 0; i < shippingAddressList.size(); i++ )
			{
				formattedArrayList.add( formatAddressAsStringForHTML( ((Address)shippingAddressList.get( i ) ) ) );
			}
		}

		return formattedArrayList;
	}

    public String getBillingAddressAsStringForHTMLWithBreaks()
    {
        String returnStr = "";
        if( this.getBillingAddress() != null)
        {
            returnStr = this.getBillingAddressAsString().trim().replaceAll("\n","<BR>");
        }
        return returnStr;
    }
    public String getShippingAddressAsStringForHTMLWithBreaks()
    {
        String returnStr = "";
        if( this.getShippingAddress() != null)
        {
            returnStr = this.getShippingAddressAsString().trim().replaceAll("\n","<BR>");
        }
        return returnStr;
    }

    public String getFinanceHoldAsString()
    {
        int reasonCode = this.getFinanceHold();
        String returnStr = "";
        switch ( reasonCode )
        {
            case -1:
                returnStr = "X";
                break;
            case 0:
                returnStr = "Approved";
                break;
            default:
                returnStr = Integer.toString(reasonCode);
                break;
        }
        return returnStr;
    }
    public String getOperationsHoldAsString()
    {
        int reasonCode = this.getOperationsHold();
        String returnStr = "";
        switch ( reasonCode )
        {
            case -1:
                returnStr = "X";
                break;
            case 0:
                returnStr = "Approved";
                break;
            default:
                returnStr = Integer.toString(reasonCode);
                break;
        }
        return returnStr;
    }
	/**
	 * @return
	 */
	public String getPath()
	{
		return path;
	}

	/**
	 * @param string
	 */
	public void setPath(String string)
	{
		path = string;
	}

    public int getShippingMethodId() {
        return shippingMethodId;
    }

    public void setShippingMethodId(int shippingMethodId) {
        this.shippingMethodId = shippingMethodId;
    }

    public String getPayTerms() {
        return payTerms;
    }

    public void setPayTerms(String payTerms) {
        this.payTerms = payTerms;
    }

    public ArrayList getShippingMethodsList() {
        return shippingMethodsList;
    }

    public void setShippingMethodsList(ArrayList shippingMethodsList) {
        this.shippingMethodsList = shippingMethodsList;
    }

    public ArrayList getPayTermsList() {
        return payTermsList;
    }

    public void setPayTermsList(ArrayList payTermsList) {
        this.payTermsList = payTermsList;
    }

    public String getNewShippingMethod() {
        return newShippingMethod;
    }

    public void setNewShippingMethod(String newShippingMethod) {
        this.newShippingMethod = newShippingMethod;
    }

    public String getNewShippingAccount() {
        return newShippingAccount;
    }

    public void setNewShippingAccount(String newShippingAccount) {
        this.newShippingAccount = newShippingAccount;
    }

    public String getNewPayTerms() {
        return newPayTerms;
    }

    public void setNewPayTerms(String newPayTerms) {
        this.newPayTerms = newPayTerms;
    }

    public String getShipMethod() {
        return shipMethod;
    }

    public void setShipMethod(String shipMethod) {
        this.shipMethod = shipMethod;
    }


    public String getVendorAddressId() {
        return vendorAddressId;
    }

    public void setVendorAddressId(String vendorAddressId) {
        this.vendorAddressId = vendorAddressId;
    }

    public ArrayList getShippingVendorAddressList() {
        return shippingVendorAddressList;
    }

    public void setShippingVendorAddressList(ArrayList shippingVendorAddressList) {
        this.shippingVendorAddressList = shippingVendorAddressList;
    }
    public ArrayList getFormattedShippingVendorAddressList()
	{
		ArrayList formattedArrayList = new ArrayList();

		if( shippingVendorAddressList != null && shippingVendorAddressList.isEmpty() == false )
		{
			for( int i = 0; i < shippingVendorAddressList.size(); i++ )
			{
				formattedArrayList.add( formatAddressAsStringForHTML( ((Address)shippingVendorAddressList.get( i ) ) ) );
			}
		}

		return formattedArrayList;
	}

    public ArrayList getShippingVendorsList() {
        return shippingVendorsList;
    }

    public void setShippingVendorsList(ArrayList shippingVendorsList) {
        this.shippingVendorsList = shippingVendorsList;
    }
}

